package me.rjy.second.screen.demo

import android.app.Activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.rjy.second.screen.demo.databinding.LayoutSecondDisplayBinding

class SecondDisplayActivity : AppCompatActivity() {
    private var mJob: Job? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = LayoutSecondDisplayBinding.inflate(layoutInflater)
        binding.pageTitle.text = "Activity"
        setContentView(binding.root)

        var countDown = 30
        mJob = coroutineMain.launch {
            while (countDown-- > 0) {
                binding.tbCountDown.text = countDown.toString()
                delay(1000)
            }
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        activityList.forEach {
            it.finish()
        }
        activityList.add(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        activityList.remove(this)
        mJob?.cancel()
        mJob = null
    }

    companion object {
        private val activityList: MutableList<Activity> = mutableListOf()
    }
}